﻿

import Request from '../../base/request.js?t=202511041006'
import Storage from '../../base/storage.js?t=202511041006'
import { dealTime, queryURLParams, IgetDeptList, getRecentYears, getMonthList, getBrowserInfo } from "../../base/utils.js?t=202511041006"
const Composition = (proxy) => {

    const generateh = (text, isShort) => {
        if (isShort) {
            return h('div', {
                style: "width:100px;overflow:hidden;white-space: nowrap;text-overflow: ellipsis; -o-text-overflow: ellipsis;cursor:pointer"
            }, { default: () => text })
        } else {
            return h('div', {
                style: "width:300px;height:auto;"
            }, { default: () => text })
        }

    }

    const data = reactive({
        messageTest: "",
        proxy: "",
        popTitle: "",
        cIndex: 1,
        tabList: [],
        queryParams: {
            Title: null,
            DepartmentId:null
        },
        planId: null,
        isAllPass: false,
        orecord: null,

        urlinfo:{},
        Ids: [],
        sYearsList:[],
        plform: {

        },
        statusList: [],
        opType: true,
        popShow: false,
        pagination: {
            total: 0,
            current: 1,
            pageSize: 10,
            loading: true,
            showTotal: total => `总共 ${total} 项`,
            defaultPageSize: 10,
            showSizeChanger: true, // 是否显示pagesize选择
            showQuickJumper: false, // 是否显示跳转窗
        },

        deptList: [

        ],

        yearList: [

        ],
        monthList: [

        ],

        DepartmentTypeList: [],
        columns2: [
            {
                title: '序号',
                dataIndex: 'index',
                key: 'index',
                align: 'center',
                width: 80,
                customRender: ({ index }) => {
                    return (params.pagination.value.current - 1) * params.pagination.value.pageSize + index + 1
                },
            },
            {
                title: '年度',
                dataIndex: 'Years',
                key: 'Years',
                align: 'center'

            },
            {
                title: '标题',
                dataIndex: 'Title',
                key: 'Title',
                align: 'center'

            },
     
            {
                title: '部门',
                dataIndex: 'DepartmentName',
                key: 'DepartmentName',
                align: 'center'
            },
            {
                title: '上传人',
                dataIndex: 'CreateUserName',
                key: 'CreateUserName',
                align: 'center',
                customRender: ({ record }) => {
                    return `${record.CreateUserName}(${record.UpdateUser})`
                },
                

            },
            {
                title: '变动情况',
                dataIndex: 'DepartmentTypeName',
                key: 'DepartmentTypeName',
                align: 'center',
                customRender: function ({ text, record, index, column }) {

                    let hdiv=(content) => h('div', {
                        style: `max-width: 600px;
                          `
                    }, {
                        default: () => content
                    })

                 
                    const tag = (text, desc)=>h(antd.Tag, {
                        color: "#108ee9",
                        style: desc?"cursor:pointer":""
                    }, { default: () => text })


                    const pop = (text,desc) => h(antd.Popover, {
                        title: '分级情况',
                        content: hdiv(desc) ,
                    }, () => [tag(text, desc)]);


                    return h('div', {
                        style: "display:flex;width:120px;height:auto;margin:0 auto;justify-content: center;"
                    }, { default: () => [pop('总数:' + record.Total, `A级数量：${record.Total_A}，B级数量：${record.Total_B}，C级数量：${record.Total_C}，D级数量：${record.Total_D}`), pop('新增:' + record.IncreaseCount, record.IncreaseDescription), pop('删减:' + record.ReduceCount, record.ReduceDescription)] })
                },

            },
            {
                title: '更新时间',
                dataIndex: 'UpdateTime',
                key: 'UpdateTime',
                align: 'center',
                customRender: function ({ text }) {
                    return dealTime(text, 'YYYY-MM-DD HH:mm')
                }

            },
            {
                title: '类型',
                dataIndex: 'Type',
                key: 'Type',
                align: 'center',
                customRender: function ({ text }) {
                    return h(antd.Tag, {
                        color: text == 1 ? '#108ee9' : '#003366'
                    }, () => text ? text == 1 ? '部门级' : '公司级' : '未知')
                }

            },
            {
                title: '操作',
                key: 'action',
                align: 'center',
                customRender: function ({ text, record, index, column }) {


                    const FDBtn = h(antd.Popconfirm, {
                        title: `提示：【${record.Years}${record.DepartmentName}${record.Title}】，删除后将无法恢复，请您确认。`,
                        onConfirm: () => methods.handleDelete(record, 'IOHS')

                    }, { default: () => [delBtn] })
                  
                    let detailBtn = h(antd.Button, {
                        type: "link",
                        onClick: () => {
                            methods.handleDetail(record, 'IOHS')
                        }
                    }, { default: () => '详情' })

                    let editBtn = h(antd.Button, {
                        type: "link",
                        onClick: () => {
                            methods.handleEdit(record,'IOHS')
                        }
                    }, { default: () => '编辑' })


                    let delBtn = h(antd.Button, {
                        type: "link",
                        danger:true
                    }, { default: () => '删除' })

                    return h('div', {
                    },
                        { default: () => [editBtn,FDBtn,detailBtn] }
                    );

                }
            },
        ],

        dataSource2: [],

    })
    let params = toRefs(data)
    const methods = {
        tchange: (e) => {
            methods.getTableList(true)
            params.queryParams.value = {

            }
        },
        toAdd: (target) => {
            window.open(`/SafetySystem/IOHSADD?target=${target}`, "_self")
        },
        handleOk: () => {
            params.popShow.value = false

        },
        handleDetail: (item,name) => {
            window.open(`/SafetySystem/IOHSADD?target=${name + '|' + item.ModuleId}&id=${item.Id}&type=9`, "_self")

        },
        handleEdit: (item, name) => {
            window.open(`/SafetySystem/IOHSADD?target=${name + '|' + item.ModuleId}&id=${item.Id}&type=1`, "_self")

        },
        handleDelete: (item, name) => {
            Request({
                url: "/SafetySystem/DeleteModuleForm",
                method: 'post',
                data: {
                    Id:item.Id
                }
            }).then(res => {
                params.messageTest.value.success(res.Msg);
                methods.getTableList(true)
            })

        },
        GetDepartmentList: async () => {
            let res = await IgetDeptList(Request)
            params.deptList.value = res
        },


        rowSelection: {
            onChange: (selectedRowKeys, selectedRows) => {

                params.Ids.value = selectedRowKeys

            },
            getCheckboxProps: record => ({
                disabled: record.Status != 3

            })
        },




        filterOption: (val, type) => {
            return `${type.label}`.includes(`${val || ''}`)
        },


        handleTableChange: (page) => {
            const { current, pageSize } = page
            params.pagination.value.current = current;
            params.pagination.value.pageSize = pageSize;
            methods.getTableList()
        },
        IGetSearchYearsList: () => {
            Request({
                url: "/SafetySystem/GetSearchYearsList",
                method: 'post',
                data: {
                    ModuleId: params.urlinfo.value.module,
                }
            }).then(res => {
                params.sYearsList.value = res.Data.map(e => ({
                    label: e,
                    value:e
                }))

            })
        },
        getTableList: async (isRresh) => {
            if (isRresh) {
                params.pagination.value.current = 1
            }
            params.pagination.value.loading = true
            Request({
                url: "/SafetySystem/GetModuleFormPageList",
                method: 'post',
                params: {
                    page: params.pagination.value.current,
                    pageSize: params.pagination.value.pageSize
                },
                data: {
                    model: {
                        ModuleId: params.urlinfo.value.module,
                        "Title": params.queryParams.value.Title,
                        "DepartmentId": params.queryParams.value.DepartmentId
                    } 
 
                }
            }).then(res => {
                params.dataSource2.value = res.Data.Data
                params.pagination.value.total = res.Data?.TotalItemCount
                params.pagination.value.loading = false
            })

        },

  
        init: () => {

            getBrowserInfo().catch(err => { console.log(err) })
            if (proxy) {
                params.proxy.value = proxy
            }
            params.urlinfo.value = queryURLParams()
            params.yearList.value = getRecentYears()
            params.monthList.value = getMonthList()
            const { message } = antd
            params.messageTest.value = message
            params.messageTest.value.config({
                top: `10vh`,
                duration: 7,
                maxCount: 1,
                rtl: true,
            });
          
            setTimeout(() => {
                let info = Storage.get('AccountInfo', true)
                if (info && info.DepartmentId == 20) {
                    params.cIndex.value = 1
                    params.tabList.value = [{ label: "危险源识别表", value: 1 }]

                } else {
                    params.cIndex.value = 1
                    params.tabList.value = [{ label: "危险源识别表", value: 1 }]

                }

                Request({
                    url: "/Account/createJwtToken",
                    method: 'post',
                    data: {
                        "userName": info && info.UserNo
                    }
                }).then( async res => {
                    Storage.set('token', res.Data,true)
                    await methods.GetDepartmentList()
                    methods.getTableList(true)

                    methods.IGetSearchYearsList()
        

                })
            }, 400)

        },

    }



    const template = {
        test: {
            template: `<div @click="add">+</div> <div>{{testnum}}</div>`,

            setup() {

                return {
                    ...params,
                    ...methods,
                };
            }

        }
    }
    return {
        ...params,
        ...methods,
        template
    }
}

export default Composition